﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccManagerKw.Common;
using DevExpress.XtraEditors;
using Model;
using Model.Interfaces;
using Utilities;

namespace AccManagerKw.Masters
{
    public partial class FrmUserMaster : FrmBaseMaster, ICrud
    {
        private ClsCommonFn _oCommFn = new ClsCommonFn();
        private UserMaster _row = new UserMaster();
        public FrmUserMaster()
        {
            InitializeComponent();
        }
        private void FrmUserMaster_Shown(object sender, EventArgs e)
        {
            _oCommFn.GroupControl_Setting(gclMain);
            _oCommFn.GroupControl_Setting(gclOpen);

            var context = new AccManagerEntities();
            lucRoles.Properties.DataSource = context.UserRoles.ToList();
            lucRoles.Properties.DisplayMember = "RoleName";
            lucRoles.Properties.ValueMember = "RoleId";
        }
        private void txtPwd_TextChanged(object sender, EventArgs e)
        {
            var txt = sender as TextEdit;
            if (txt != null)
            {
                var pwd1 = txtPwd.Text.Trim();
                var pwd2 = txtPwd2.Text.Trim();
                if (pwd1 != pwd2)
                    ccErrorProvider1.SetError(txt, "Passwords Do Not Match.");
                else
                {
                    ccErrorProvider1.SetError(txtPwd, "");
                    ccErrorProvider1.SetError(txtPwd2, "");
                }
            }
        }



        public bool OperationSave()
        {
            try
            {
                if (IsValid())
                {
                    if (Mode == ClsGlobalVarz.Mode.Add && _oCommFn.ValidateCrud(Mode))
                    {
                        OperationUser();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode == ClsGlobalVarz.Mode.Edit && _oCommFn.ValidateCrud(Mode))
                    {
                        OperationUser();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode != ClsGlobalVarz.Mode.Add && Mode != ClsGlobalVarz.Mode.Edit)
                    {
                        _oCommFn.WarningMsg("Check Mode");
                        return false;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }
        private void OperationUser()
        {
            using (var context = new AccManagerEntities())
            {
                var oUserMaster = new UserMaster();
                if (Mode == ClsGlobalVarz.Mode.Add)
                {
                    oUserMaster.UserId = context.UserMasters.ToList().Count > 0
                        ? context.UserMasters.Max(x => x.UserId) + 1 : 1;
                }
                else if (Mode == ClsGlobalVarz.Mode.Edit)
                {
                    oUserMaster = context.UserMasters.Single(x => x.UserId == _row.UserId);
                }

                oUserMaster.UserName = txtUserName.Text;
                oUserMaster.Pwd = Encryption.Encrypt(txtPwd.Text, ClsGlobalVarz.EncKey);
                oUserMaster.RoleId = lucRoles.Value;
                oUserMaster.IsActive = "Y";

                if (Mode == ClsGlobalVarz.Mode.Add)
                    context.UserMasters.Add(oUserMaster);
                context.SaveChanges();
            }
        }

        public void OperationOpen()
        {
            var context = new AccManagerEntities();
            gcUsers.DataSource = context.UserMasters.Where(x => x.IsActive == "Y").ToList();
            gclOpen.BringToFront();
        }
        private void gvUsers_DoubleClick(object sender, EventArgs e)
        {
            _row = (UserMaster)gvUsers.GetFocusedRow();
            txtUserName.Text = _row.UserName;
            txtPwd.Text = Encryption.Decrypt(_row.Pwd, ClsGlobalVarz.EncKey);
            txtPwd2.Text = Encryption.Decrypt(_row.Pwd, ClsGlobalVarz.EncKey);
            lucRoles.EditValue = _row.RoleId;

            _oCommFn.DisableControls(gclMain, true);
            gclMain.BringToFront();
        }


        public void OperationEdit()
        {
            if (_oCommFn.ValidateOpenRecord(_row.UserId))
            {
                _oCommFn.DisableControls(gclMain, false);
                SetMode(ClsGlobalVarz.Mode.Edit);
            }
        }

        public bool OperationDelete()
        {
            try
            {
                if (_oCommFn.ValidateOpenRecord(_row.UserId))
                {
                    SetMode(ClsGlobalVarz.Mode.Delete);
                    if (_oCommFn.ValidateCrud(Mode))
                    {
                        using (var context = new AccManagerEntities())
                        {
                            var oUserMaster = context.UserMasters.Single(x => x.UserId == _row.UserId);
                            oUserMaster.IsActive = "N";
                            context.SaveChanges();
                            _oCommFn.MsgBoxCrud(Mode);
                        }
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }

        public void ResetData()
        {
            _oCommFn.ClearFields(this);
            _oCommFn.DisableControls(gclMain, false);
            gclMain.BringToFront();
            SetMode(ClsGlobalVarz.Mode.Add);
            txtPwd.Properties.ReadOnly = false;
            txtPwd2.Properties.ReadOnly = false;
        }

        public bool IsValid()
        {
            ccDxValidationProvider1.Validate();
            var x = ccDxValidationProvider1.GetInvalidControls().Count <= 0;
            var y = ccErrorProvider1.GetError(txtPwd).Trim() == "";
            var z = ccErrorProvider1.GetError(txtPwd2).Trim() == "";
            return (x && y && z);
        }






    }
}
